package com.cbs.persistencecache;

import android.annotation.TargetApi;
import android.os.Build;
import android.util.Base64;
import com.cbs.database.Database;

import java.util.List;
import java.util.Map;

/**
 * Created by brochexu on 5/21/15.
 */
@TargetApi(Build.VERSION_CODES.FROYO)
public class DataManager {
    private Database database = null;

    public DataManager(Database database) {
        this.database = database;
        init();
    }

    public String getBuffer(String scope, String tag, String key) {
        if (scope == null) scope = "";
        if (tag == null) tag = "";
        if (key == null) key = "";
        Object result = database.scalar(String.format("select buffer from cbspersistencebuffer where scope='%s' and tag='%s' and key='%s'", scope, tag, key));
        if (result == null) {
            return "";
        } else {
            byte[] bytes = result.toString().getBytes();
            return new String(Base64.decode(bytes, Base64.DEFAULT));
        }
    }

    public int getSize() {
        List<Map<String, Object>> result = database.query("select buffer from cbspersistencebuffer");
        int length = 0;
        for (Map<String, Object> item : result) {
            length += item.get("buffer").toString().getBytes().length;
        }
        length = length / 1024;
        return length;

    }

    public void setBuffer(String scope, String tag, String key, String value) {
        if (scope == null) scope = "";
        if (tag == null) tag = "";
        if (key == null) key = "";
        if (value == null) value = "";
        byte[] bytes = value.getBytes();
        value = new String(Base64.encode(bytes, Base64.DEFAULT));
        if (existed(scope, tag, key)) {
            database.execute(String.format("update cbspersistencebuffer set buffer='%s' where scope='%s' and tag='%s' and key='%s'", value, scope, tag, key));
        } else {
            database.execute(String.format("insert into cbspersistencebuffer(scope, tag, key, buffer) values('%s', '%s', '%s', '%s')", scope, tag, key, value));
        }
    }

    public void clear() {
        database.execute("delete from cbspersistencebuffer");
    }

    public void removeBuffer(String scope) {
        if (scope == null) scope = "";
        database.execute(String.format("delete from cbspersistencebuffer where scope='%s'", scope));
    }

    public void removeBuffer(String scope, String tag) {
        if (scope == null) scope = "";
        if (tag == null) tag = "";
        database.execute(String.format("delete from cbspersistencebuffer where scope='%s' and tag='%s'", scope, tag));
    }

    public void removeBuffer(String scope, String tag, String key) {
        if (scope == null) scope = "";
        if (tag == null) tag = "";
        if (key == null) key = "";
        database.execute(String.format("delete from cbspersistencebuffer where scope='%s' and tag='%s' and key='%s'", scope, tag, key));
    }

    private void init() {
        int count = ((Integer) database.scalar("select count(1) from sqlite_master where type='table' and name='cbspersistencebuffer'"));
        if (count <= 0) {
            database.execute("create table 'cbspersistencebuffer' (id integer not null, scope varchar(64,0), tag varchar(128,0), key varchar(64,0), buffer ntext, primary key(id))");
        }
    }

    private boolean existed(String scope, String tag, String key) {
        if (scope == null) scope = "";
        if (tag == null) tag = "";
        if (key == null) key = "";
        int count = (Integer) database.scalar(String.format("select count(1) from cbspersistencebuffer where tag='%s' and key='%s' and scope='%s'", tag, key, scope));
        return count > 0;
    }
}
